package edu.ccut.saturn.studio.logic.model.xml.loader;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnStartIterationNode;

public class StartIterationLoader extends NodeLoader {

	public List<Shape> loadNodes(Element root) {

		NodeList startIterationNodes = root
				.getElementsByTagName(SaturnStartIterationNode.KEY_NODE_STAETITERATION);

		List<Shape> shapes = new ArrayList<Shape>(startIterationNodes
				.getLength());

		for (int i = 0; i < startIterationNodes.getLength(); ++i) {
			SaturnStartIterationNode saturnStartIterationNode = new SaturnStartIterationNode();
			Node startiterationNode = startIterationNodes.item(i);
			String shapeId = super.getNodeAttributeValue(startiterationNode,
					Shape.KEY_ATTRIBUTE_SHAPE_ID);
			String nextId = super.getNodeAttributeValue(startiterationNode,
					Shape.KEY_ATTRIBUTE_SHAPE_NEXT_ID);
			String name = super.getNodeElementValue(startiterationNode,
					SaturnStartIterationNode.SHOW_STAETITERATION_NODE_NAME);
			String description = super
					.getNodeElementValue(
							startiterationNode,
							SaturnStartIterationNode.SHOW_STAETITERATION_NODE_DESCRITPION);
			String iteratorlistkey = super.getNodeElementValue(
					startiterationNode,
					SaturnStartIterationNode.SHOW_STAETITERATION_NODE_LIST_KEY);
			String iteratoritemkey = super.getNodeElementValue(
					startiterationNode,
					SaturnStartIterationNode.SHOW_STAETITERATION_NODE_ITEM_KEY);
			String endid = super.getNodeElementValue(startiterationNode,
					SaturnStartIterationNode.SHOW_STAETITERATION_NODE_END_ID);
			
			Point location = super.getLocation(startiterationNode);
			Dimension size = new Dimension(50, 70);
			
			saturnStartIterationNode.setNode(startiterationNode);
			saturnStartIterationNode.setId(shapeId);
			saturnStartIterationNode.setLocation(location);
			saturnStartIterationNode.setName(name);
			saturnStartIterationNode.setNextId(nextId);
			saturnStartIterationNode.setDescription(description);
			saturnStartIterationNode.setSize(size);
			saturnStartIterationNode.setListkey(iteratorlistkey);
			saturnStartIterationNode.setItemkey(iteratoritemkey);
			saturnStartIterationNode.setEndid(endid);
			shapes.add(saturnStartIterationNode);
		}

		return shapes;
	}
}
